package com.auxgroup.smarthome.openapi;


import com.auxgroup.smarthome.utils.HttpClientUtil;

import java.util.HashMap;
import java.util.Map;

/***
 *  机智云设备操作事件枚举
 * @author kevinchen
 *
 */
public enum OpenUrl {
    /**
     * 创建用户（用户注册）
     */
    CREATE_USER("users", new String[][]{{"X-Gizwits-Application-Id", "%s"}}, HttpClientUtil.MethodType.METHOD_POST),

    /**
     * 用户登录
     */
    LOGIN("login", new String[][]{{"X-Gizwits-Application-Id", "%s"}}, HttpClientUtil.MethodType.METHOD_POST),

    /**
     * 重置密码
     */
    resetPassword("reset_password", new String[][]{{"X-Gizwits-Application-Id", "%s"}}, HttpClientUtil.MethodType.METHOD_POST),
    /**
     * 修改密码
     */
    modifyPassword("users", new String[][]{{"X-Gizwits-Application-Id", "%s"}, {"X-Gizwits-User-token", "%s"}}, HttpClientUtil.MethodType.METHOD_PUT),

    /**
     * 修改用户信息
     */
    UPDATE_USER("users", new String[][]{{"X-Gizwits-Application-Id", "%s"}, {"X-Gizwits-User-token", "%s"}}, HttpClientUtil.MethodType.METHOD_PUT),
    /**
     * 获取用户信息
     */
    GET_USER("users", new String[][]{{"X-Gizwits-Application-Id", "%s"}, {"X-Gizwits-User-token", "%s"}}, HttpClientUtil.MethodType.METHOD_GET),

    /**
     * 微信端 匿名注册 获取uid与token
     */
    anonymousLogin("users", new String[][]{{"X-Gizwits-Application-Id", "%s"}}, HttpClientUtil.MethodType.METHOD_POST),

    /**
     * 通过mac地址绑定设备
     */
    bindMac("bind_mac", new String[][]{{"X-Gizwits-Application-Id", "%s"}, {"X-Gizwits-User-token", "%s"}, {"X-Gizwits-Timestamp", "%s"}, {"X-Gizwits-Signature", "%s"}}, HttpClientUtil.MethodType.METHOD_POST),

    /**
     * 通过did绑定设备
     */
    bindDid("bindings?show_detail=1", new String[][]{{"X-Gizwits-Application-Id", "%s"}, {"X-Gizwits-User-token", "%s"}}, HttpClientUtil.MethodType.METHOD_POST),

    /**
     * 通过passcode绑定设备（或修改设备名称）
     */
    bindPasscode("bindings", new String[][]{{"X-Gizwits-Application-Id", "%s"}, {"X-Gizwits-User-token", "%s"}}, HttpClientUtil.MethodType.METHOD_GET),

    /**
     * 修改绑定信息
     */
    updateBind("bindings/%s", new String[][]{{"X-Gizwits-Application-Id", "%s"}, {"X-Gizwits-User-token", "%s"}}, HttpClientUtil.MethodType.METHOD_GET),

    /**
     * 获取用户当前绑定的设备
     */
    deviceList("bindings", new String[][]{{"X-Gizwits-Application-Id", "%s"}, {"X-Gizwits-User-token", "%s"}}, HttpClientUtil.MethodType.METHOD_GET),

    /**
     * 解除设备绑定
     */
    unbind("bindings", new String[][]{{"X-Gizwits-Application-Id", "%s"}, {"X-Gizwits-User-token", "%s"}}, HttpClientUtil.MethodType.METHOD_DELETE),

    /**
     * 获取设备运行状态
     */
    devdata("app/devdata/%s/latest", new String[][]{{"X-Gizwits-Application-Id", "%s"}}, HttpClientUtil.MethodType.METHOD_GET),

    /**
     * 获取token
     */
    request_token("request_token", new String[][]{{"X-Gizwits-Application-Id", "%s"}, {"X-Gizwits-Application-Auth", "%s"}}, HttpClientUtil.MethodType.METHOD_POST),

    /**
     * 获取设备聚合数据
     */
    aggData("devdata/%s/agg_data", new String[][]{{"X-Gizwits-Application-Id", "%s"}, {"X-Gizwits-User-token", "%s"}}, HttpClientUtil.MethodType.METHOD_POST),


    /**
     * 获取短信验证码
     */
    sms_code("sms_code", new String[][]{{"X-Gizwits-Application-Id", "%s"}, {"X-Gizwits-Application-Token", "%s"}}, HttpClientUtil.MethodType.METHOD_POST);


    private static final String baseUrl = "http://smthomeapi.auxgroup.com/app/";

    private String uri;

    private String[][] headers;

    private HttpClientUtil.MethodType methodType;

    OpenUrl(String uri, String[][] headers, HttpClientUtil.MethodType methodType) {
        this.uri = baseUrl + uri;
        this.headers = headers;
        this.methodType = methodType;
    }

    public String getUri() {
        return uri;
    }

    public String getUri(String... params) {
        return String.format(uri, params);
    }

    public Map<String, String> getHeaderMap(String... params) {
        if (headers.length != 0 && headers.length != params.length) {
            throw new RuntimeException("配置或参数错误！");
        }
        Map<String, String> headerMap = new HashMap<String, String>();
        for (int i = 0; i < headers.length; i++) {
            String[] head = headers[i];
            if (head.length != 2) {
                throw new RuntimeException("配 置 错 误！");
            }
            headerMap.put(head[0], params[i]);
        }
        return headerMap;
    }

    public HttpClientUtil.MethodType getMethodType() {
        return methodType;
    }

}
